import type { MenuItem,BreadcrumbItem } from '@/types/menu';

/**
 * 根据当前路径查找完整面包屑路径
 * @param menuTree 菜单树
 * @param currentPath 当前路由路径
 */
export function findBreadcrumbPath(
  menuTree: MenuItem[],
  currentPath: string
): BreadcrumbItem[] {
  console.log('menuTree', menuTree);
  if (!menuTree || menuTree.length === 0) {
    return [];
  }
  const result: BreadcrumbItem[] = [];
  
  const finder = (menuItems: MenuItem[], path: string): boolean => {
    return menuItems.some(item => {
      // 匹配当前项
      if (item.path === path) {
        result.unshift({
          title: item.meta?.title || item.title,
          path: item.path,
          icon: item.meta?.icon
        });
        return true;
      }
      
      // 递归查找子项
      if (item.children && finder(item.children, path)) {
        result.unshift({
          title: item.meta?.title || item.title,
          path: item.path,
          icon: item.meta?.icon
        });
        return true;
      }
      
      return false;
    });
  };

  finder(menuTree, currentPath);
  console.log(result);
  return result;
}